package com.imooc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.enums.CommentLevel;
import com.imooc.mapper.*;
import com.imooc.pojo.*;
import com.imooc.service.ItemsService;
import com.imooc.utils.DesensitizationUtil;
import com.imooc.utils.PagedGridResult;
import com.imooc.vo.CommentsLevelCountsVo;
import com.imooc.vo.ItemCommentsVo;
import com.imooc.vo.QueryItemsVo;
import com.imooc.vo.ShopCartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * @Author wangke
 * @Description 商品详情数据服务
 * @Date 2021/7/15 12:44
 * @Version 1.0
 */
@Service
public class ItemsServiceImpl implements ItemsService {
    @Autowired
    private ItemsMapper itemsMapper;
    @Autowired
    private ItemsParamMapper itemsParamMapper;
    @Autowired
    private ItemsSpecMapper itemsSpecMapper;
    @Autowired
    private ItemsImgMapper itemsImgMapper;
    @Autowired
    private ItemsCommentsMapper itemsCommentsMapper;
    @Autowired
    private ItemsCommentsCustomMapper itemsCommentsCustomMapper;
    @Autowired
    private ItemsCustomMapper itemsCustomMapper;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Items getItem(String itemId) {

        return itemsMapper.selectByPrimaryKey(itemId);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public ItemsParam getItemParams(String itemId) {
        Example example = new Example(ItemsParam.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);
        ItemsParam itemsParam = itemsParamMapper.selectOneByExample(example);
        return itemsParam;
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsSpec> getItemSpecList(String itemId) {
        Example example = new Example(ItemsSpec.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId",itemId);
        List<ItemsSpec> itemsSpecs = itemsSpecMapper.selectByExample(example);
        return itemsSpecs;
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<ItemsImg> getItemImgList(String itemId) {
        Example example = new Example(ItemsImg.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("itemId", itemId);
        List<ItemsImg> itemsImgs = itemsImgMapper.selectByExample(example);
        return itemsImgs;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public CommentsLevelCountsVo queryCommentLevelCounts(String itemId) {
        ItemsComments itemsComment = new ItemsComments();
        itemsComment.setItemId(itemId);
        itemsComment.setCommentLevel(CommentLevel.GOOD.type);
        int goodCounts = itemsCommentsMapper.selectCount(itemsComment);
        itemsComment.setCommentLevel(CommentLevel.NORMAL.type);
        int normalCounts = itemsCommentsMapper.selectCount(itemsComment);
        itemsComment.setCommentLevel(CommentLevel.BAD.type);
        int badCounts = itemsCommentsMapper.selectCount(itemsComment);
        CommentsLevelCountsVo countsVo = new CommentsLevelCountsVo();
        countsVo.setGoodCounts(goodCounts);
        countsVo.setNormalCounts(normalCounts);
        countsVo.setBadCounts(badCounts);
        countsVo.setTotalCounts(goodCounts+normalCounts+badCounts);
        return countsVo;
    }

    //带分页查询商品评论详情
    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult queryPagedComments(String itemId, Integer commentLevel, Integer page, Integer pageSize) {
        //构建分页条件,拦截sql 重新拼接语句
        PageHelper.startPage(page,pageSize);
        //构建查询条件;
        HashMap<String, Object> params = new HashMap<>();
        if(itemId!=null)
        params.put("itemId",itemId);
        params.put("level",commentLevel);
        List<ItemCommentsVo> itemCommentsVos = itemsCommentsCustomMapper.queryItemComments(params);
        //信息脱敏处理
        for (ItemCommentsVo itemCommentsVo : itemCommentsVos) {
            itemCommentsVo.setNickname(DesensitizationUtil.commonDisplay(itemCommentsVo.getNickname()));
            //头像不做处理
            //itemCommentsVo.setUserFace(DesensitizationUtil.commonDisplay(itemCommentsVo.getUserFace()));
        }
        PagedGridResult pagedGridResult = getPagedGridResult(itemCommentsVos, page);
        return pagedGridResult;
    }

    @Override  //条件查询商品信息
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult queryConditionItems(String keywords, String sort, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        HashMap<String, Object> params = new HashMap<>();
        params.put("keywords",keywords);
        params.put("sort",sort);
        List<QueryItemsVo> queryItemsVos = itemsCustomMapper.queryByConditionItems(params);
        PagedGridResult pagedGridResult = getPagedGridResult(queryItemsVos, page);
        return pagedGridResult;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public PagedGridResult searchItemsByCategory(Integer catId, String sort, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        HashMap<String, Object> params = new HashMap<>();
        params.put("catId",catId);
        params.put("sort",sort);
        List<QueryItemsVo> queryItemsVos = itemsCustomMapper.queryItemsByCategory(params);
        PagedGridResult pagedGridResult = getPagedGridResult(queryItemsVos, page);
        return pagedGridResult;
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public List<ShopCartVo> queryItemsBySpec(String specIds) {
        String[] ids = specIds.split(",");
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, ids);
        List<ShopCartVo> shopCartVos = itemsCustomMapper.queryItemsBySpec(list);
        return shopCartVos;
    }

    //重新构建返回格式
    public PagedGridResult getPagedGridResult(List<?> list, Integer page){
        PageInfo<?> pageInfo = new PageInfo<>(list);
        PagedGridResult pagedGridResult = new PagedGridResult();
        pagedGridResult.setPage(page);
        pagedGridResult.setRecords(pageInfo.getTotal());
        pagedGridResult.setRows(pageInfo.getList());
        pagedGridResult.setTotal(pageInfo.getPages());
        return pagedGridResult;

    }

}
